import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_case/case_detail/state.dart';
import 'package:seedfour/toolkit/typedef/function.dart';

import '../../../../models/case_detail_entity.dart';

class ContractWidget extends StatelessWidget {
  const ContractWidget({
    Key? key,
    required this.data,
    required this.onDownloadFile,
    required this.onDeleteFile,
    required this.onApplyOfSeal,
    required this.onUploadContract,
  }) : super(key: key);

  final CaseDetailState data;
  final ParamSingleCallback onDownloadFile;
  final ParamSingleCallback onDeleteFile;
  final ParamVoidCallback onApplyOfSeal;
  final ParamVoidCallback onUploadContract;

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Container(
        margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(kRadiusMedium),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            data.caseDetail.value!.caseInfo!.isSignedContract!
                ? _buildContracts(data.caseContractList)
                : _buildNotContract(),
          ],
        ),
      ),
    );
  }

  Widget _buildContractItem(int index, CaseContractFile file) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: Text(
              '${index + 1}. ${file.name}',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                  fontSize: kFontSizeSmall, color: AppColor.colorFontBlue),
            ),
          ),
          IconButton(
            onPressed: () => onDeleteFile(index),
            icon: const Icon(
              Icons.close,
              size: 20,
              color: AppColor.colorFontBlue,
            ),
          ),
          IconButton(
            onPressed: () => onDownloadFile(index),
            icon: const Icon(
              Icons.file_download_outlined,
              size: 20,
              color: AppColor.colorFontBlue,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildContracts(List<CaseContractFile> files) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: EdgeInsets.all(kSpaceMedium),
          child: Text(
            '案件委托合同',
            style: TextStyle(
              fontSize: kFontSizeMedium,
              color: AppColor.colorFontBlack,
              fontWeight: AppFont.fontMedium,
            ),
          ),
        ),
        const Divider(),
        SizedBox(height: kSpaceMedium),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
          child: Text(
            files.isNotEmpty
                ? '当前已上传的合同文件：${files.length}'
                : '请及时上传本案委托合同，方便用印审批及结案审批',
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontBlack,
            ),
          ),
        ),
        SizedBox(height: kSpaceMini),
        ...files.asMap().entries.map((e) => _buildContractItem(e.key, e.value)),
        if (data.caseStatus.value == 0) ...[
          const Divider(),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Expanded(
                child: FilledButton(
                  onPressed: onApplyOfSeal,
                  style: ElevatedButton.styleFrom(
                    foregroundColor: AppColor.colorFontBlue,
                    backgroundColor: Colors.transparent,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(kRadiusSmall),
                    ),
                    padding: EdgeInsets.zero,
                  ),
                  child: Center(
                    child: Text(
                      '合同用印申请',
                      style: TextStyle(
                        fontSize: kFontSizeSmall,
                        color: AppColor.colorFontBlue,
                      ),
                    ),
                  ),
                ),
              ),
              Container(
                width: 0.5.w,
                height: 24.w,
                color: AppColor.colorLineGrey,
              ),
              Expanded(
                child: FilledButton(
                  onPressed: onUploadContract,
                  style: ElevatedButton.styleFrom(
                    foregroundColor: AppColor.colorFontBlue,
                    backgroundColor: Colors.transparent,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(kRadiusSmall),
                    ),
                    padding: EdgeInsets.zero,
                  ),
                  child: Center(
                    child: Text(
                      '上传合同',
                      style: TextStyle(
                        fontSize: kFontSizeSmall,
                        color: AppColor.colorFontBlue,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ],
      ],
    );
  }

  Widget _buildNotContract() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: EdgeInsets.all(kSpaceMedium),
          child: Text(
            '案件委托合同',
            style: TextStyle(
              fontSize: kFontSizeMedium,
              color: AppColor.colorFontBlack,
              fontWeight: AppFont.fontMedium,
            ),
          ),
        ),
        const Divider(),
        SizedBox(height: kSpaceMedium),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
          child: Text(
            '本案还未进行合同用印申请，无法上传合同',
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontBlack,
            ),
          ),
        ),
        SizedBox(height: kSpaceMini),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
          child: Text(
            '请进行合同用印申请并处理完成后，上传签署版的合同！',
            style: TextStyle(
              fontSize: kFontSizeMini,
              color: AppColor.colorFontGrey,
            ),
          ),
        ),
        SizedBox(height: kSpaceSmall),
        Center(
          child: FilledButton(
            onPressed: onApplyOfSeal,
            style: ElevatedButton.styleFrom(
              foregroundColor: AppColor.colorFontBlue,
              backgroundColor: Colors.transparent,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(kRadiusSmall),
              ),
              padding: EdgeInsets.zero,
            ),
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
              child: SvgPicture.asset(
                'assets/buttons/ic_seal_upload.svg',
                width: 141.w,
                height: 21.w,
              ),
            ),
          ),
        ),
      ],
    );
  }
}
